<template>
  <div :style="style">
    <el-card class="title">
      <div class="card-header">
        <span> {{ title || '列表' }}</span>
      </div>
    </el-card>
    <el-card>
      <template #header>
        <div class="table-header">
          <epx-toolbar :items="toolbar" :row="selected" @command="onToolbar"></epx-toolbar>
          <epx-form
            v-if="search"
            v-model="query_"
            v-bind="search"
            :inline="true"
            lable-position="left"
            layout="Simple"
            :reset="false"
            @submit="onQuery"
          ></epx-form>
        </div>
      </template>
      <el-container class="el-container">
        <el-main class="el-main">
          <epx-table v-bind="$attrs" :rows="rows">
            <el-table-column v-if="embedment">
              <template #default="{ row }">
                <epx-toolbar :row="row" :items="embedment" @command="onEmbedment(row, $event)">
                  <slot name="embedment"></slot>
                </epx-toolbar>
              </template>
            </el-table-column>
          </epx-table>
        </el-main>
        <el-footer class="el-footer">
          <!--          <el-alert-->
          <!--            :title="description"-->
          <!--            type="info"-->
          <!--            show-icon-->
          <!--            :closable="false"-->
          <!--            class="description"-->
          <!--          />-->
          <el-pagination
            v-if="pagination_"
            v-model:current-page="current_"
            hide-on-single-page
            :page-size="10"
            :total="pagination_.total"
            layout="total, prev, pager, next, jumper"
            background
          />
        </el-footer>
      </el-container>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
  import { Row } from '../components/elementUI';
  import { computed, ref } from 'vue';
  import { Command, CommandOptions } from '../components/core';
  import EpxToolbar from '../components/button/EpxToolbar.vue';
  import { Toolbar } from '../components/button/button';
  import EpxForm from '../components/form/EpxForm.vue';
  import { Form } from '../components/form/form';
  import { Pagination, TableResult } from '../components/table/table';
  import EpxTable from '../components/table/EpxTable.vue';

  const props = defineProps<{
    title?: string;
    description?: string;
    url: string;
    style?: unknown;
    embedment?: Toolbar['items'];
    toolbar?: Toolbar['items'];
    search?: Form;
    pagination?: Pagination;
  }>();

  const emit = defineEmits(['command']);

  const query_ = ref<Record<string, unknown>>({});

  const rows = ref<Row[]>([]);
  const selected = ref<Array<number | string | undefined>>([]);
  const pagination_ = ref<Pagination>(props.pagination || { current: 0 });

  const current_ = computed<number>({
    get: () => pagination_.value.current || 0,
    set: async (n: number) => await reset({ current: n })
  });

  const onEmbedment = (row: Row, command: CommandOptions) => emit('command', command, row);
  const onToolbar = (command: CommandOptions) =>
    emit(
      'command',
      {
        ...command,
        after: reset
      },
      selected
    );

  const reset = async (
    { query = query_.value, current = current_.value } = {} as {
      query?: Record<string, unknown>;
      current?: number;
    }
  ) => {
    const r = (await Command.Get(props.url, { query, current })) as undefined | TableResult;
    if (!r) return;

    rows.value = r.rows;
    query_.value = {};
    if (r.pagination) pagination_.value = r.pagination;
    selected.value = [];
  };

  const onQuery = async () => await reset({ query: query_.value, current: 0 });

  reset();
</script>

<script lang="ts">
  // 声明额外的选项
  // noinspection JSUnusedGlobalSymbols
  export default {
    inheritAttrs: false
  };
</script>

<style scoped>
  .title {
    margin-bottom: 15px;
  }

  .card-header {
    display: flex;
    justify-content: left;
    align-items: center;
  }

  .table-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .el-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-direction: row-reverse;
  }

  .description {
    margin-left: 20px;
    width: 50%;
  }
  .description:first-child {
    margin: 0;
  }
</style>
